﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Caching;
using KL.Store.Core.Domain;
using TN.Data;

namespace KL.Store.Data
{
    public class dbCategory
    {
        #region Get BreadCrumb

        public static string GetBreadCrumb(int CategoryId, int MainCategoryId, int HomePageCategoryId, string Language)
        {
            IList<BreadCrumb> objectList = new List<BreadCrumb>();
            SqlHelper.FillList(dbParameters.dsnStore, "SR_STOK_GRUP_LISTESI_INET_HEADER", objectList, new object[] { CategoryId, MainCategoryId, HomePageCategoryId, Language });

            return objectList.FirstOrDefault().Header;
        }

        #endregion

        #region GetMenu

        public static string GetMenu(int CategoryId)
        {
            IList<CategoryMenu> objectList = new List<CategoryMenu>();
            SqlHelper.FillList(dbParameters.dsnStore, "SPR_ESTORE_STOK_GRUP_TREE", objectList, new object[] { CategoryId });

            StringBuilder sb = new StringBuilder();
            foreach (CategoryMenu cm in objectList)
            {
                sb.Append(cm.Line);
            }

            return sb.ToString();
        }

        #endregion

        #region Get

        public static Category Get(int CategoryId)
        {
            IList<Category> objectList = new List<Category>();
            SqlHelper.FillList(dbParameters.dsnStore, "SPR_ESTORE_STOKGRUP_GET", objectList, new object[] { CategoryId });

            return objectList.FirstOrDefault();
        }

        #endregion

        #region GetByName

        public static int GetByName(string CategoryName)
        {
            int? categoryId;
            categoryId = (int?)SqlHelper.ExecuteScalar(dbParameters.dsnStore, "SPR_ESTORE_Category_GetByName", new object[] { CategoryName });
            if (categoryId.HasValue)
            {
                return (int)categoryId;
            }
            else
            {
                return -1;
            }
        }

        #endregion

        #region GetMeta

        public static CategoryMeta GetMeta(int CategoryId)
        {
            var cacheKey = "GetMeta" + CategoryId;
            if (HttpRuntime.Cache[cacheKey] != null)
            {
                return HttpRuntime.Cache[cacheKey] as CategoryMeta;
            }
            IList<CategoryMeta> objectList = new List<CategoryMeta>();
            SqlHelper.FillList(dbParameters.dsnStore, "SPR_ESTORE_Category_Get_Meta", objectList, new object[] { CategoryId });

            HttpRuntime.Cache.Add(cacheKey, objectList.FirstOrDefault(), null, DateTime.UtcNow.AddDays(1), Cache.NoSlidingExpiration, CacheItemPriority.NotRemovable, null);

            return objectList.FirstOrDefault();
        }

        #endregion

        #region SaveCategoryMeta

        public static int SaveCategoryMeta(CategoryMeta cm)
        {
            int id = (int)SqlHelper.ExecuteScalar(dbParameters.dsnStore, "SPR_ESTORE_Category_SaveCategoryMeta", new object[] { cm.CategoryId, cm.WebTitle, cm.Description, cm.Keywords, cm.Canonical });
            return id;
        }

        #endregion

        #region GetList

        public static IList<Category> GetList(int CategoryId)
        {
            IList<Category> objectList = new List<Category>();
            SqlHelper.FillList(dbParameters.dsnStore, "SPR_ESTORE_STOKGRUP_LIST", objectList, new object[] { CategoryId });

            return objectList;
        }

        #endregion

        #region GetByType

        public static IList<Category> GetByType(string GrupKod)
        {
            IList<Category> objectList = new List<Category>();
            SqlHelper.FillList(dbParameters.dsnStore, "SPR_ESTORE_Category_List_By_Type", objectList, new object[] { GrupKod });

            return objectList;
        }

        #endregion

        #region UpdateGroupCode

        public static bool UpdateGroupCode(int CategoryId, string GrupKod)
        {
            SqlHelper.ExecuteScalar(dbParameters.dsnStore, "SPR_ESTORE_Category_Update_Grupkod", new object[] { CategoryId, GrupKod });
            return true;
        }

        #endregion

        #region SetBannerActive

        public static int SetBannerActive(int CategoryId)
        {
            int result = (int)SqlHelper.ExecuteScalar(dbParameters.dsnStore, "SPR_ESTORE_Category_SetBannerActive", new object[] { CategoryId });
            return result;
        }

        #endregion

    }
}
